package com.orhon.expert.user.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 文件工具类
 * @author 	池永福
 * @date	2018年4月12日 下午7:53:35
 */
public class FileUtils {
	
	

	
	/**
	 * 获取三级目录   例如:/2018/5/systemTime/
	 * @param basicPath 基础路径
	 * @param systemTime 时间戳
	 * @param date Calendar对象
	 * @param sep  路径通配符
	 * @return
	 */
	public static String getBasicPath(Long systemTime,Calendar date,String sep){
		//Calendar date = Calendar.getInstance();
		int year = date.get(Calendar.YEAR);
		int month = date.get(Calendar.MONTH);
		return year+sep+month+sep+systemTime+sep;
	}

	/**
	 * 将服务器文件下载到本地
	 * @param filePath	文件路径
	 * @param response	响应
	 * @param request	请求
	 * @param fileName	文件名称
	 * @param fileType	文件类型
	 * @return
	 */
	public static boolean downLoadFile(String filePath,
			HttpServletResponse response, HttpServletRequest request,
			String fileName, String fileType) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			response.setContentType("text/html;charset=utf-8");
			request.setCharacterEncoding("UTF-8");
			String downLoadPath = filePath;
			long fileLength = new File(downLoadPath).length();
			if ("pdf".equals(fileType)) {
				response.setContentType("application/pdf;charset=UTF-8");
			} else if ("xls".equals(fileType)) {
				response.setContentType("application/x-msdownload;");
//				response.setContentType("application/msexcel;charset=UTF-8");
			} else if ("doc".equals(fileType)) {
				response.setContentType("application/msword;charset=UTF-8");
			}else if("zip".equals(fileType)){
				response.setContentType("application/zip;charset=UTF-8");
			}else if("docx".equals(fileType)){
				response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=UTF-8");
			}else if("xlsx".equals(fileType)){
				response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			}
			response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
			response.setHeader("Content-Length", String.valueOf(fileLength));
			bis = new BufferedInputStream(new FileInputStream(downLoadPath));
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] data=new byte[2048];
			int index=0;
			while((index=bis.read(data))!=-1){
				bos.write(data, 0, index);
			}
		} catch (Exception e) {
		} finally {
			if (bis != null)
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (bos != null)
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return true;
	}
	public static boolean downLoadFile(String filePath,String fileName,String fileType,
			HttpServletResponse response, HttpServletRequest request) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			response.setContentType("text/html;charset=utf-8");
			request.setCharacterEncoding("UTF-8");
			String downLoadPath = filePath;
			long fileLength = new File(downLoadPath).length();
			if ("pdf".equals(fileType)) {
				response.setContentType("application/pdf;charset=UTF-8");
			} else if ("xls".equals(fileType)) {
				response.setContentType("application/x-msdownload;");
//				response.setContentType("application/msexcel;charset=UTF-8");
			} else if ("doc".equals(fileType)) {
				response.setContentType("application/msword;charset=UTF-8");
			}else if("zip".equals(fileType)){
				response.setContentType("application/zip;charset=UTF-8");
			}else if("docx".equals(fileType)){
				response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=UTF-8");
			}
			response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
			response.setHeader("Content-Length", String.valueOf(fileLength));
			bis = new BufferedInputStream(new FileInputStream(downLoadPath));
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (Exception e) {
		} finally {
			
			if (bos != null)
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (bis != null)
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return true;
	}
}
